/*
 * This file is part of Ember.
 *
 * Ember is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ember is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Ember.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) Grant Tyra 2010
 * granttyra@gmail.com
*/

#include "ember_console.h"
#include "../libember/ember_types.h"
#include "../libember/ember_string.h"
#include "../libember/ember_dictionary.h"
#include <stdio.h>
#include <stdlib.h>

EmberObject *ember_console_writeln() {
	EmberObject *argdict = EmberCallStackRetrieve("argdict");
	EmberDictionary *dict = EmberObjectGetAttribute(argdict, "dict");
	printf("%s\n", EmberStringValue(EmberDictionaryGet(dict, "line")));
	return EmberNull();
}

EmberObject *ember_console_readln() {
	EmberObject *line;
	char *buffer, c;
	int size;

	buffer = NULL;
	c = getchar();
	size = 1;

	while (c != '\n') {
		EmberStringExpand(&buffer, size);
		buffer[size - 1] = c;
		c = getchar();
		size++;
	}

	EmberStringExpand(&buffer, size);
	buffer[size - 1] = '\0'; 

	line = EmberObjectCreateString(buffer);
    EmberStringDestroy(buffer);
    buffer = NULL;
    
    return line;
}
